/*
 *  Axolotl - Alternate persistence for Grails
 *  Copyright (C) 2008  Juanjo García Latre
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package com.googlecode.axolotl.grails;

import java.util.ArrayList;
import java.util.List;

import org.codehaus.groovy.grails.commons.DomainClassArtefactHandler;
import org.codehaus.groovy.grails.commons.GrailsApplication;
import org.codehaus.groovy.grails.commons.GrailsDomainClass;
import org.codehaus.groovy.grails.commons.GrailsDomainClassProperty;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import com.googlecode.axolotl.lazy.LazyEntity;


public abstract class GrailsDomainUtils {

	
	public static Object getIdentifierValue(GrailsApplication application, Object entity) {
		Class clazz = entity.getClass();
		return getIdentifierValue( getDomainClass( application, clazz ), entity );
	}
	
	
	public static Object getIdentifierValue(GrailsDomainClass domainClass, Object entity) {
		BeanWrapper wrapper = (entity instanceof BeanWrapper) ? (BeanWrapper) entity : new BeanWrapperImpl(entity);
		return wrapper.getPropertyValue( domainClass.getIdentifier().getName() );			
	}

	
	public static List getPersistentValues(GrailsDomainClass domainClass, Object entity) {
		BeanWrapper wrapper = (entity instanceof BeanWrapper) ? (BeanWrapper) entity : new BeanWrapperImpl(entity);
		List<Object> values = new ArrayList<Object>();			
		for (GrailsDomainClassProperty property : getPersistentProperties(domainClass)) {
			Object value = wrapper.getPropertyValue( property.getName() );
			if ((property.isManyToOne() || property.isOneToOne()) && value != null) {
				if (LazyEntity.isLazy(value)) {
					value = LazyEntity.getId(value);
				}
				else {
					value = GrailsDomainUtils.getIdentifierValue(
								property.getReferencedDomainClass(), value);
				}
			}
			values.add(value);
		}
		return values;
	}	

	
	public static GrailsDomainClassProperty[] getPersistentProperties(GrailsDomainClass domainClass) {
		List<GrailsDomainClassProperty> properties = new ArrayList<GrailsDomainClassProperty>();
		for (GrailsDomainClassProperty property: domainClass.getPersistentProperties()) {
			if (!property.isOneToMany() && !property.isManyToMany()) {
				properties.add(property);
			}
		}
		return properties.toArray(new GrailsDomainClassProperty[properties.size()]);
	}

	
	public static GrailsDomainClass getDomainClass(GrailsApplication application, Class clazz) {
		return (GrailsDomainClass) application.getArtefact(DomainClassArtefactHandler.TYPE, clazz.getName());
	}

	
	public static boolean isDomainObject(GrailsApplication application, Object object) {
		return application.isArtefactOfType(DomainClassArtefactHandler.TYPE, object.getClass());
	}
}
